﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Yoozoo.Managers.ResourceManagement
{
    public class ChannelConfig : ScriptableObject
    {
        /// <summary>
        /// android渠道列表
        /// </summary>
        [SerializeField] public List<BuildAndroidUnit> AndroidChannelConfigs;

        /// <summary>
        /// ios渠道列表
        /// </summary>
        [SerializeField] public List<BuildIOSUnit> IosChannelConfigs;

        /// <summary>
        /// windows渠道列表
        /// </summary>
        [SerializeField] public List<BuildAndroidUnit> WindowsChannelConfigs;
    }

    public class BuildConfigManager
    {
        public static string[] GetBuildChannels()
        {
            Dictionary<string, BuildAppConfigUnit> dic = LoadConfig();

            List<string> list = new List<string>();
            foreach (var v in dic.Keys)
            {
                list.Add(v);
            }

            return list.ToArray();
        }

        public static BuildAppConfigUnit SetPlayerSettings(string channel)
        {
            Dictionary<string, BuildAppConfigUnit> dic = LoadConfig();

            if (dic.ContainsKey(channel))
            {
                dic[channel].SetPlayerSettings();
            }

            return dic[channel];
        }

        public static Dictionary<string, BuildAppConfigUnit> LoadConfig()
        {
            Dictionary<string, BuildAppConfigUnit> dic = new Dictionary<string, BuildAppConfigUnit>();
            string path = TypeUtility.GetConfigurationPath<ChannelConfigPathAttribute>();
            ChannelConfig config = AssetDatabase.LoadAssetAtPath<ChannelConfig>(path);
            if (config == null)
            {
                EditorUtility.DisplayDialog("information", "需要配置channelconfig文件", "Ok");
                return null;
            }
#if UNITY_IOS
            List<BuildIOSUnit> list = config.IosChannelConfigs;
#elif UNITY_ANDROID
            List<BuildAndroidUnit> list = config.AndroidChannelConfigs;
#elif UNITY_STANDALONE
            List<BuildAndroidUnit> list = config.WindowsChannelConfigs;
#endif

            for (int i = 0; i < list.Count; i++)
            {
                BuildAppConfigUnit unit = list[i];
                dic[unit.channelName] = unit;
            }

            return dic;
        }
    }
}